{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=8):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'input': {'data': [0.02158712, 0.17120789, 0.42253968, -0.34874392, 0.04062585, 0.47916386, 0.684427, -0.69498906, 0.06485479, 0.05675407, -0.07646271, 0.18419409, -0.79339215, 0.62722166, -0.85550321, -0.36241177, 0.85582755, 0.68180441, -0.32636201, -0.21848271, 0.30283706, 0.32128667, -0.05237423, 0.06603501, 0.80225016, 0.99431247, 0.82962931, -0.75036757, 0.311414, 0.45841489, 0.51261021, 0.02276411, -0.82829494, 0.92948606, 0.47251296, -0.65135147, -0.39246035, -0.26382678, -0.29026049, 0.31517715, -0.82461071, -0.2283719, 0.69612321, -0.83480008, 0.96956959, -0.04238643, -0.39236961, -0.27375073, 0.31817232, -0.26229144, -0.41973438, 0.82439776, -0.96024608, 0.88288849, 0.22662472, 0.42280432, -0.06176646, -0.27379721, -0.74020505, -0.78652315, 0.34077241, -0.4854114, -0.94292376, -0.69879077, -0.45936408, 0.07486736, 0.12277373, 0.44663933, -0.2374805, 0.97628004, 0.8363243, 0.93503332, 0.29394737, -0.68209785, 0.95477247, -0.07327856, 0.50103735, 0.91777335, 0.6739928, -0.98499775, 0.61051427, 0.39329428, -0.12069, -0.44325231, -0.81229626, 0.2400612, -0.6014919, 0.3950817, 0.9199326, 0.73138346, 0.48931682, -0.13941714, 0.66200411, -0.56300023, -0.74614386, -0.5024158, 0.09216436, -0.74613148, -0.10437111, -0.98804565, -0.38953607, -0.75173589, 0.10544912, 0.2122157, 0.36553923, -0.14460125, 0.75693161, 0.02540242, -0.80083957, -0.45071968, -0.22808028, -0.13830507, 0.75961451, 0.21697387, 0.19385144, -0.81843405, -0.47749522, -0.47903825, -0.76566692, -0.82896292, -0.51813879, 0.37840462, -0.36735574, -0.07504603, -0.81150799, -0.1402414, -0.86849378, 0.33481197, -0.51653972, -0.66723411, -0.89837662, -0.97345217, -0.38707172, -0.08084839, -0.79660717, 0.77982, 0.27035192, 0.25028201, -0.97524904, -0.92697161, 0.63661278, 0.42884333, 0.36808762, 0.52321376, 0.63645692, 0.37568512, -0.32183286, 0.65335452, -0.00800893, 0.07030834, -0.2509481, 0.34497345, -0.11285549, -0.75627752, -0.87781018, -0.7973496, 0.82729233, 0.51446942, -0.12950208, 0.43420662, 0.75511833, -0.93647487, -0.35605641, -0.52631582, -0.90144472, -0.97732854, 0.17179967, 0.44534986, 0.48705451, 0.95239958, -0.07300467, -0.57763669, 0.35861235, 0.22819977, -0.15022505, 0.7520017, 0.14421397, -0.55360269, 0.91661729, 0.92669435, 0.72500495, 0.55358073, 0.36028805, -0.15271359, 0.64104893, 0.96855478, 0.45156663, -0.88901832, 0.48842958, -0.351093, -0.67190119, -0.43973608, -0.28708468, -0.68767828, -0.16321336, -0.21370899, -0.21174935, -0.21502596, -0.51156359, 0.59257122, -0.15697238, -0.02862129, -0.42731955, 0.34483679, -0.276822, 0.57312261, 0.7907316, 0.19361778, -0.17640237, 0.17934235, 0.55594689, 0.95585115, 0.81017922, 0.75471052, 0.79564587, -0.25556368, 0.11602279, 0.857657, -0.35001556, 0.08552007, 0.58877939, -0.64342779, -0.7977012, -0.87148681, -0.55082017, -0.0305768, -0.75059153, 0.57125314, 0.96391297, 0.39870963, -0.67978376, 0.30217196, 0.93471635, 0.24202425, -0.1976091, -0.39713328, 0.65045014, -0.48133337, -0.19923805, 0.27914001, 0.96562448, -0.75656163, 0.94165971, 0.30262302, -0.83689393, 0.22446091, 0.54344857, -0.76236893, 0.1756069, -0.54666899, -0.33326313, 0.74928084, -0.97799533, -0.75971366, -0.54297296, -0.3107184, 0.2526795, 0.71558783, 0.23286232, 0.16796822, 0.74606831, -0.59650556, 0.50078203, -0.49858879, 0.72629614, -0.39741953, -0.65975619, -0.52303856, -0.53587673, 0.9189599, 0.43290191, -0.41476647, 0.68506651, 0.70985046, -0.01356488, 0.88973841, -0.32958735, 0.62959076, 0.42988989, -0.50456319, 0.23516331, 0.17141323, 0.71106819, 0.63447829, 0.4098946, 0.61861807, 0.63511319, -0.59775147, 0.14721656, -0.19298424, 0.61788402, 0.45965692, -0.30936991, -0.13001533, -0.12806892, -0.67742868, -0.37549713, -0.83797772, 0.05130556, -0.9987987, -0.77609001, -0.1182133, 0.7011551, -0.43229115, -0.46936791, 0.86181722, -0.01025849, -0.97296949, -0.33018088, -0.03030297, -0.7594704, -0.54205711, -0.87113412, -0.30831415, -0.56692256, -0.68787692, 0.68601806, 0.1021083, 0.81493924, 0.07333344, 0.97604486, -0.62454922, -0.44193294, 0.44694708, -0.9616747, -0.80015259, 0.6833773, -0.31135211, -0.64842544, 0.56962317, 0.73264555, -0.28141575, -0.48575739, -0.22457109, -0.45576362, -0.26430444, 0.63059153, -0.53597864, 0.93952919, -0.55697326, 0.57560236, -0.11517109, -0.0349925, 0.61047474, -0.20292739, 0.9930552, 0.77601247, -0.70363136, -0.84759904, -0.7760801, -0.04363689, -0.1523109, -0.96173715, -0.76658421, 0.32023616, 0.77744943, 0.91435343, 0.7491553, 0.40378078, 0.9291371, 0.54335998, -0.2501595, -0.71173057, -0.37590035, -0.37471305, -0.4511426, 0.65954622, -0.83623269, 0.56575628, -0.90654873, 0.92988972, 0.56136784, -0.64810252, 0.99687993, -0.35394723, 0.48667097, -0.64651264, 0.3535835, 0.21073345, -0.16189519, -0.10782969, -0.3458355, 0.44622457, -0.06808724, 0.35185097, -0.70426219, -0.12378755, 0.03116178, 0.75227018, -0.63683864, -0.40304354, -0.13436577, -0.89425913, -0.41579286, -0.91484919, 0.01585965, -0.51692233, 0.14855114, 0.02891288, 0.17210021, -0.54113574, 0.3569098, -0.63639918, -0.78602205, 0.60640877, 0.26678617, 0.48439871, 0.60489156, -0.50640138, 0.42960609, 0.6026213, 0.72232948, -0.5999677, 0.5974766, -0.11468093, -0.91200886, -0.59372174, -0.75228349, 0.3525877, 0.42311969, 0.50357755, 0.8768361, -0.33447034, 0.5805673, -0.089548, -0.62483196, -0.88824033, 0.95515643, 0.86431987, -0.1635778, 0.72856758, 0.15155153, -0.72370583, -0.06956691, -0.16810678, 0.72833225, 0.2174608, -0.96397124, -0.05708696, 0.10087297, 0.03018523, -0.79591462, -0.81829274, 0.94442449, -0.73824392, 0.38574104, 0.02880237, 0.52033325, -0.19511934, 0.84133026, -0.58710165, -0.23015826, 0.63350329, 0.02299491, 0.97226814, 0.34246344, 0.64616127, -0.46018167, -0.03561687, 0.24592514, -0.85117151, -0.51333591, -0.81505806, -0.41295655, 0.51997057, 0.38011854, -0.3333831, -0.1279661, 0.6052381, -0.71539978, -0.42342126, -0.61309864, -0.95769165, -0.08200324, -0.11478403, -0.66970664, -0.28086432, -0.54861811, -0.4065818, -0.35742788, 0.39742654, -0.89230796, -0.29682554, 0.42797911, 0.82472824, -0.14035094, 0.40765659, 0.14570908, -0.83494487, -0.09076587, 0.12213856, -0.28856821, -0.61427764, -0.65316302, 0.17457161, -0.72725836, 0.73785521, 0.43001408, 0.12521799, 0.42699848, -0.02223488, -0.48986409, 0.42348567, 0.37705787, -0.7920571, -0.20642536, 0.07656763, 0.98489561, 0.95779205, -0.09303089, 0.08419748, 0.9324686], 'shape': [16, 16, 2]}, 'expected': {'data': [-2.86881304, 2.22523975, -4.3545146, 1.48329401, -3.05925322, 2.34794068, 0.89177448, -1.03126717, 1.51951337, 4.35741663, 2.93000865, -3.16733789, 1.3905549, -2.39094925, 0.09081267, -3.86763287, -3.30399418, -13.34720993, 4.84404421, -6.70168161, -6.87816238, -0.0757184, 7.40303612, 5.8596344, 11.28536701, -6.77409124, -18.21478271, 12.79656601, -7.6375041, -7.99474144, -1.76169395, -11.36316204], 'shape': [4, 4, 2]}, 'weights': [{'data': [0.02158712, 0.17120789, 0.42253968, -0.34874392, 0.04062585, 0.47916386, 0.684427, -0.69498906, 0.06485479, 0.05675407, -0.07646271, 0.18419409, -0.79339215, 0.62722166, -0.85550321, -0.36241177, 0.85582755, 0.68180441, -0.32636201, -0.21848271, 0.30283706, 0.32128667, -0.05237423, 0.06603501, 0.80225016, 0.99431247, 0.82962931, -0.75036757, 0.311414, 0.45841489, 0.51261021, 0.02276411, -0.82829494, 0.92948606, 0.47251296, -0.65135147, -0.39246035, -0.26382678, -0.29026049, 0.31517715, -0.82461071, -0.2283719, 0.69612321, -0.83480008, 0.96956959, -0.04238643, -0.39236961, -0.27375073, 0.31817232, -0.26229144, -0.41973438, 0.82439776, -0.96024608, 0.88288849, 0.22662472, 0.42280432, -0.06176646, -0.27379721, -0.74020505, -0.78652315, 0.34077241, -0.4854114, -0.94292376, -0.69879077, -0.45936408, 0.07486736, 0.12277373, 0.44663933, -0.2374805, 0.97628004, 0.8363243, 0.93503332, 0.29394737, -0.68209785, 0.95477247, -0.07327856, 0.50103735, 0.91777335, 0.6739928, -0.98499775, 0.61051427, 0.39329428, -0.12069, -0.44325231, -0.81229626, 0.2400612, -0.6014919, 0.3950817, 0.9199326, 0.73138346], 'shape': [3, 3, 2, 5]}, {'data': [-0.48639824, -0.92496192, 0.92424352, -0.09950512, 0.69720942], 'shape': [5]}, {'data': [-0.75296601, 0.1329459, 0.24902519, -0.74689724, -0.33805842, 0.40025983, -0.58853674, 0.42201389, -0.26070005, 0.77074112, -0.20293159, -0.94813153, 0.56654217, 0.48204917, -0.11305891, 0.717462, -0.75332517, -0.20832032, -0.75794837, -0.84413753, 0.2202828, -0.38153812, -0.59702398, 0.40117898, 0.13962775, -0.7188989, 0.64689064, 0.32622309, 0.85693066, -0.1302924, 0.92433632, 0.20960729, 0.74790798, -0.76553062, -0.40330047, 0.41865502, 0.30354412, -0.49448981, 0.97317699, -0.24650491, -0.90835078, 0.86187478, -0.69632747, 0.41804268, -0.08948609, 0.78532865, -0.26704036, 0.27922428, 0.41950937, 0.43554137, -0.76145759, -0.44619172, 0.14672987, 0.71791966, 0.9592894, -0.86354504, 0.9478882, 0.36554527, 0.65163129, -0.43568867, 0.16995745, 0.46346893, 0.04185147, -0.4283142, -0.36808308, 0.17801533, 0.27674865, -0.24439779, -0.84303801, -0.29673877, -0.4798807, 0.25954352, -0.87481117, -0.49939911, -0.59371498, -0.80568408, 0.3904619, 0.19041621, -0.73004874, 0.0566803, 0.27657418, -0.25725775, -0.42477486, -0.86015376, 0.60231593, 0.66309417, 0.54957863, -0.59699293, 0.91824779, -0.94695282, 0.30404785, -0.16712675, 0.34380573, 0.51340269, 0.60767655, 0.19452629, 0.84619071, 0.72617641, -0.24614, 0.69354071, 0.71601165, -0.94718703, 0.43537978, 0.07599353, 0.53860197, -0.46882507, -0.40148747, 0.98429472, -0.97511419, -0.06557814, -0.58241562, 0.28552787, 0.82696814, 0.8536786, 0.18731846, 0.77914382, 0.56463531, -0.14772215, 0.22886158, -0.62778697, -0.66654899, -0.0146146, -0.61494712, 0.36385595, -0.98302122, -0.87258611, 0.15844176, 0.63534401, 0.51187408, -0.51452512, -0.64346452, -0.79889111, 0.82419348, -0.40437639, 0.33364714, 0.54376018, 0.06984339, -0.0242119, 0.96697208, -0.47544343, -0.01147073, -0.49840709, -0.74400373, -0.4551518, -0.6132647, -0.74282173, -0.13318394, -0.97408323, -0.19101788, 0.62476475, -0.60763976, 0.07467859, -0.6614908, 0.75610284, 0.18691369, -0.17736694, 0.7441209, -0.6204425, -0.06407962, 0.04427596, 0.10158513, 0.77458475, -0.24314802, 0.84561735, 0.25363529, -0.37789056, -0.4619769, 0.45394559, 0.81488295, 0.558249, -0.99855486, 0.63840649, -0.7014548, -0.06218036, -0.8743715, -0.70593254, -0.81592926, -0.49631579, 0.48796294, -0.080183], 'shape': [3, 3, 5, 4]}, {'data': [-0.32191781, 0.41066255, -0.77584414, -0.65172762], 'shape': [4]}, {'data': [-0.00887914, -0.53562911, 0.50047629, -0.44375227, -0.37006178, 0.7668487, 0.34893679, 0.53087203], 'shape': [1, 1, 4, 2]}, {'data': [-0.85285892, 0.94677404], 'shape': [2]}, {'data': [-0.79381514, -0.51847477, -0.57986202, 0.30253107, 0.68038586, 0.69219795, -0.68275837, -0.75212293, 0.74333866, 0.22112702, 0.04256682, 0.21579059, -0.25609851, 0.02292363, 0.15754606, 0.53503814, -0.59173592, 0.44302679, -0.51414983, 0.46761372, -0.39106157, 0.99358419, 0.96520689, 0.50918181, 0.52173884, 0.78688692, -0.70790857, 0.14466889, 0.54330439, -0.87319778, -0.94606638, -0.75665158, 0.84844139, 0.27863242, 0.4303456, -0.91569222, -0.20614657, -0.98476926, -0.72996885, -0.27266045, -0.57555441, 0.67564621, -0.65868721, -0.40186854, 0.79010241, 0.44700929, -0.29191267, -0.65516076, 0.12894563, 0.91988545, -0.1391523, 0.27157785, -0.10789259, 0.43975886, 0.74283569, -0.28490471, -0.48639941, 0.85075338, -0.81370668, -0.22424567, 0.7159208, -0.87077198, -0.03510482, -0.7156976, -0.90396306, 0.79063565, 0.3026989, -0.36311283, -0.16915686, 0.62942384, -0.51503355, -0.98267274, -0.21451168, -0.30185229, 0.57350173, -0.16869609, 0.53972382, -0.36414598, -0.71938024, 0.18448331, -0.10309479, -0.25118757, 0.66084491, -0.11953897, -0.55756288, -0.35333783, -0.44342037, -0.56116758, 0.88651895, 0.04293462, -0.88032649, 0.37260274, -0.74548709, 0.89610256, -0.38442087, 0.44082085, 0.27291362, 0.95550225, 0.58802535, -0.87283807, -0.7435972, -0.51806246, -0.08010922, -0.06513987, 0.51913148, 0.30759969, -0.08966757, -0.12412922, 0.42503469, 0.38117708, -0.50647053, -0.8415914, 0.83373264, -0.65921933, 0.93280635, -0.01399168, 0.74527568, -0.8300006, 0.29965608, -0.33408651, -0.09057249, -0.64850758, 0.77594894, -0.85786738, -0.78603925, 0.08410126, 0.08332478, -0.59688571, 0.53707683, 0.46623004, 0.49985474, -0.92801392, 0.14745883, 0.11669858, -0.78036795, 0.78507705, -0.94166445, -0.58253671, -0.05226325, -0.13378748, 0.60503848, -0.16569612, 0.20297312, 0.96175295, 0.93218268, -0.84469948, 0.00479579, -0.17193796, 0.89658624, -0.2751383], 'shape': [5, 5, 2, 3]}, {'data': [0.47686103, -0.90213748, 0.15637747], 'shape': [3]}, {'data': [0.8243882, -0.06839876, 0.01867725, -0.59505459, -0.70637933, -0.9214682, -0.1452791, 0.70276539, 0.75002398, 0.94870098, 0.75355477, 0.34506739, 0.49772878, 0.56431927, -0.28864624, 0.92229265, 0.1655839, 0.32337357, -0.26322544, 0.23776651, 0.99482932, -0.04324538, -0.5033122, 0.25007188, 0.96250842, 0.84931639, 0.33198803, -0.0737646, -0.87678214, 0.80774117, 0.12426598, -0.77951207, 0.73930116, -0.64831785, -0.6031486, 0.11815687, 0.42589103, -0.53052032, 0.98277407, 0.35886592, 0.16537683, -0.75060873, -0.59847299, 0.36939739, 0.40920177, -0.46326589, -0.14648736, 0.06823803, 0.56623019, 0.3959679, 0.55955409, 0.65707637, -0.58698858, 0.2701089], 'shape': [3, 3, 3, 2]}, {'data': [0.18080605, -0.69955432], 'shape': [2]}, {'data': [0.67231182, -0.79974829, 0.48203651, -0.83399305, 0.82585207, -0.93934408, -0.22724803, -0.28793067], 'shape': [1, 1, 2, 4]}, {'data': [-0.59066382, 0.40813233, 0.24637542, -0.79664285], 'shape': [4]}, {'data': [0.82068067, -0.23464819, 0.5264436, 0.89359276, 0.56700929, -0.76109692, -0.07944819, 0.05268229, 0.17571857, 0.43162231, -0.37104992, 0.80675975, 0.08373824, -0.57776257, -0.93826059, -0.33488752, 0.44407794, 0.68445615, 0.14076429, -0.80498504, -0.77219055, 0.85988883, 0.89427737, -0.45260519, -0.68543705, -0.26314859, -0.59386404, -0.85469658, -0.92019612, 0.44256737, -0.00615336, 0.7262208, -0.62947116, 0.28650862, -0.44836829, -0.51299378, -0.15835562, -0.12308793, 0.15899427, -0.37759787, 0.33529705, 0.1641291, -0.34772007, 0.94884948, 0.95955787, 0.03189801, 0.42464648, 0.31847751, 0.2973738, -0.42540307, 0.270022, 0.20621489, 0.86976143, 0.26075301, 0.58456663, 0.89221612, -0.00826881, -0.1945271, -0.51556837, -0.22491037, 0.14408836, -0.75810017, 0.21367603, 0.42613528, -0.70216579, -0.98310822, 0.54617292, 0.19359363, 0.02855712, -0.58672519, -0.25394887, -0.29797568], 'shape': [3, 3, 4, 2]}, {'data': [0.00500462, -0.58641725], 'shape': [2]}]}\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (16, 16, 2)\n",
    "\n",
    "conv_0 = Conv2D(5, 3, 3, activation='relu', border_mode='same', subsample=(2, 2), dim_ordering='tf', bias=True)\n",
    "conv_1 = Conv2D(4, 3, 3, activation='linear', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "conv_2 = Conv2D(2, 1, 1, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "conv_3 = Conv2D(3, 5, 5, activation='relu', border_mode='same', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "conv_4 = Conv2D(2, 3, 3, activation='linear', border_mode='same', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "conv_5 = Conv2D(4, 1, 1, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "conv_6 = Conv2D(2, 3, 3, activation='linear', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = conv_0(input_layer)\n",
    "x = conv_1(x)\n",
    "x = conv_2(x)\n",
    "x = conv_3(x)\n",
    "x = conv_4(x)\n",
    "x = conv_5(x)\n",
    "output_layer = conv_6(x)\n",
    "model = Model(input=input_layer, output=output_layer)\n",
    "\n",
    "np.random.seed(3000)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(3000 + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "\n",
    "print({\n",
    "    'input': {'data': format_decimal(data_in.ravel().tolist()), 'shape': list(data_in_shape)},\n",
    "    'weights': [{'data': format_decimal(weights[i].ravel().tolist()), 'shape': list(weights[i].shape)} for i in range(len(weights))],\n",
    "    'expected': {'data': format_decimal(result[0].ravel().tolist()), 'shape': list(result[0].shape)}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
